package com.example.vp2;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;

import androidx.navigation.ui.AppBarConfiguration;

import com.example.vp2.databinding.ActivityMainBinding;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.nio.ByteBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import ml.exp.glquad;
import ml.exp.MediaSystem;
import ml.exp.Media;

public class MainActivity extends AppCompatActivity {

    //先弄一个android 播放视频的例子
    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;

    private int mediaid = 0;//播放的mediaID

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());

        //布置一个空的View
        FrameLayout view = new FrameLayout(this);
        setContentView(view);

        //增加一行log
        TextView textview = new TextView(this);
        {


            FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);
            layout.topMargin = 100;
            textview.setLayoutParams(layout);
            textview.setText("视频播放插件 Java部分验证");
            view.addView(textview);
        }

        Context context = this;
        //Start MediaSystemOnly
        {
            Button btn = new Button(this);
            FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(600, 150);
            layout.topMargin = 200;
            btn.setLayoutParams(layout);
            btn.setText("Start MediaSystem");
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    MediaSystem.System_Start(context);//启动GLThread

                    textview.setText("State=" + MediaSystem.System_IsOpen() + "Mem=" + Runtime.getRuntime().totalMemory() / 1024 / 1024);

                }
            });
            view.addView(btn);
        }
        {//GC
            Button btn = new Button(this);
            FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(200, 150);
            layout.topMargin = 200;
            layout.leftMargin = 700;
            btn.setLayoutParams(layout);
            btn.setText("GC");
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    System.gc();
                    textview.setText("State=" + MediaSystem.System_IsOpen() + "Mem=" + Runtime.getRuntime().totalMemory() / 1024 / 1024);

                }
            });
            view.addView(btn);
        }
        //Open Video
        {
            Button btn = new Button(this);
            FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(400, 150);
            layout.topMargin = 400;
            btn.setLayoutParams(layout);
            btn.setText("Open Video");
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mediaid <= 0) {
                        MediaSystem.System_Start(context);//启动GLThread
                        mediaid = MediaSystem.Media_Open(1, "trailer.mp4", true);
                        MediaSystem.Media_SetVolume(mediaid, 0.5f);
                        MediaSystem.Media_SetLoop(mediaid, true);

                        //打开一个media 的 视频流
                        //MediaSystem.Media_OpenView(mediaid, 0, 0);//


                    }
                    textview.setText("State=" + MediaSystem.System_IsOpen() + "Mem=" + Runtime.getRuntime().totalMemory() / 1024 / 1024);
                }
            });
            view.addView(btn);
        }
        //Open View
        {
            Button btn = new Button(this);
            FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(400, 150);
            layout.topMargin = 400;
            layout.leftMargin = 500;
            btn.setLayoutParams(layout);
            btn.setText("Open View");
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mediaid > 0) {
                        if (!MediaSystem.Media_HasView(mediaid)) {
                            //打开一个media 的 视频流
                            MediaSystem.Media_OpenView(mediaid, 0, 0);//
                        }
                    }

                    textview.setText("State=" + MediaSystem.System_IsOpen() + "Mem=" + Runtime.getRuntime().totalMemory() / 1024 / 1024);

                }
            });
            view.addView(btn);
        }
        //Open Video And View
        {
            Button btn = new Button(this);
            FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(600, 150);
            layout.topMargin = 600;
            btn.setLayoutParams(layout);
            btn.setText("Open Video And View");
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mediaid <= 0) {
                        MediaSystem.System_Start(context);//启动GLThread
                        mediaid = MediaSystem.Media_Open(1, "trailer.mp4", true);
                        MediaSystem.Media_SetVolume(mediaid, 0.5f);
                        MediaSystem.Media_SetLoop(mediaid, true);


                    }
                    if (mediaid > 0) {
                        if (!MediaSystem.Media_HasView(mediaid)) {
                            //打开一个media 的 视频流
                            MediaSystem.Media_OpenView(mediaid, 0, 0);//
                        }
                    }

                    textview.setText("State=" + MediaSystem.System_IsOpen() + "Mem=" + Runtime.getRuntime().totalMemory() / 1024 / 1024);
                }
            });
            view.addView(btn);
        }
        {//Close All
            Button btn = new Button(this);
            FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(300, 150);
            layout.topMargin = 600;
            layout.leftMargin = 600;
            btn.setLayoutParams(layout);
            btn.setText("Close All");
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //这里枷锁，因为SurfaceView 在另一个线程，不要在渲染中突然得到一个奇怪的状态

                    MediaSystem.System_Stop();

                    mediaid = 0;
                    textview.setText("State=" + MediaSystem.System_IsOpen() + "Mem=" + Runtime.getRuntime().totalMemory() / 1024 / 1024);
                }
            });
            view.addView(btn);
        }
        GLSurfaceView glview = new GLSurfaceView(this);
        {
            FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400);
            layout.topMargin = 900;
            glview.setLayoutParams(layout);
            view.addView(glview);

        }


        Log.w("==E==", "Ready to Play");


        //GL播放方式，用GLSurfaceView 的 render时调用 UpdateTexImage
        glquad quad = new glquad();
        glview.setEGLContextClientVersion(2);//init gles20


        glview.setRenderer(new GLSurfaceView.Renderer() {
            @Override
            public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
                Log.w("==E==", "gl onSurfaceCreated");


                quad.Init();


            }

            int outwidth;
            int outheight;
            boolean inited = false;

            @Override
            public void onSurfaceChanged(GL10 gl10, int width, int height) {
                Log.w("==E==", "gl onSurfaceChanged");
                outwidth = width;
                outheight = height;
                GLES20.glViewport(0, 0, outwidth, outheight);

            }

            @Override
            public void onDrawFrame(GL10 gl10) {

                //屏蔽渲染
                if (MediaSystem.System_IsOpen()) {//系统是否打开
                    if (mediaid > 0) {
                        //&& MediaSystem.Media_HasView(mediaid)) {//视图是否存在
                        ByteBuffer buffer = MediaSystem.Media_GetViewBuffer(mediaid);
                        int w = MediaSystem.Media_GetViewWidth(mediaid);
                        int h = MediaSystem.Media_GetViewHeight(mediaid);
                        //这里已经实现了DrawQuad，这个就和Buffer解耦了
                        if (buffer != null && w > 0 && h > 0) {
                            quad.SetPixel(buffer, w, h);
                        }
                    }
                }

                //GLES20.glViewport(0, 0, outwidth, outheight);

                quad.Draw2Screen_New();


            }
        });


    }

    @Override
    protected void onPause() {
        super.onPause();
        if (MediaSystem.System_IsOpen()) {
            MediaSystem.System_Pause();
            Log.w("==E==", "onPause");
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (MediaSystem.System_IsOpen()) {
            MediaSystem.System_Resume();
            Log.w("==E==", "onResume");
        }

    }
}

